﻿function format_selection(v, txtBoxId) {

    if (navigator.product == 'Gecko') {
        var textarea = document.getElementById(txtBoxId);
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;
        var sel = textarea.value.substring(start, end);
        var replace = "[" + v + "]" + sel + "[/" + v + "]";
        // Here we are replacing the selected text with this one
        textarea.value = textarea.value.substring(0, start) + replace + textarea.value.substring(end, len);
    }

    else {
        var str = document.selection.createRange().text;
        document.getElementById(txtBoxId).focus();
        var sel = document.selection.createRange();
        sel.text = "[" + v + "]" + str + "[/" + v + "]";
    }
    return;
}

function format_selectionInternal(v, txtBoxId) {
    if (navigator.product == 'Gecko') {
        var textarea = document.getElementById(txtBoxId);
        var len = textarea.value.length;
        var start = textarea.selectionStart;
        var end = textarea.selectionEnd;
        var sel = textarea.value.substring(start, end);
        var replace = "[" + v + ":" + sel + "]";
        // Here we are replacing the selected text with this one
        textarea.value = textarea.value.substring(0, start) + replace + textarea.value.substring(end, len);
    }

    else {
        var str = document.selection.createRange().text;
        document.getElementById(txtBoxId).focus();
        var sel = document.selection.createRange();
        sel.text = "[" + v + ":" + str + "]";
    }
    return;

}

function checkHtmlInput(sender, arguments) {
    var myarray = new Array();
    var found = false;
    var mx = -1;
    var currentValuePointer = 0;
    var nextValuePointer = 1;

    for (var lx = 0; lx < arguments.Value.length; lx++) {
        var curChar = arguments.Value.substring(lx, lx + 1);
        if (curChar == '[') {
            mx += 1;
            myarray[mx] = '';
            myarray[mx] += curChar;
            while (curChar != ']' && lx < arguments.Value.length) {
                lx++
                curChar = arguments.Value.substring(lx, lx + 1);
                myarray[mx] += curChar;
            }
        }
    }
    if (myarray.length % 2 != 0) {
        //alert("missing open or closing tag");
        var MissingTag = document.getElementById(sender.MissingTag);
        sender.innerHTML = MissingTag.value; ;
        arguments.IsValid = false;
        return;

    }
    else {
        for (lx = 1; lx < myarray.length; lx++) {
            //1. Check if the number of records in the array is odd:
            //yes-> continue No: give an error
            //2.Start with second element of the array
            var NextValue = myarray[lx + 1];
            var currentValue = myarray[lx];
            var lastValue = myarray[lx - 1];
            switch (currentValue) {
                case '[/B]': //If closer found. check if the priv is the opener
                    if (lastValue == '[B]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2; //array now decrease with two elements
                    }
                    break;
                case '[/U]':
                    if (lastValue == '[U]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/I]':
                    if (lastValue == '[I]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/QUOTE]':
                    if (lastValue == '[QUOTE]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/PIC]':
                    if (lastValue == '[PIC]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/LINK]':
                    if (lastValue == '[LINK]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/YOUTUBE]':
                    if (lastValue == '[YOUTUBE]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/PLAYER]':
                    if (lastValue == '[PLAYER]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/TEAM]':
                    if (lastValue == '[TEAM]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/COACH]':
                    if (lastValue == '[COACH]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/MATCH]':
                    if (lastValue == '[MATCH]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/COUNTRY]':
                    if (lastValue == '[COUNTRY]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/STADIUM]':
                    if (lastValue == '[STADIUM]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                case '[/POST]':
                    if (lastValue == '[POST]') {
                        myarray.splice(lx - 1, 2); //delete the two elements
                        lx = lx - 2;
                    }
                    break;
                default:
            }

        }

        if (myarray.length == 0) {
            // alert("good input");
            arguments.IsValid = true;
            return;
        }
        else {
            //alert("one of the tags is not ok");
            var NotOkTag = document.getElementById(sender.NotOkTag);
            sender.innerHTML = NotOkTag.value; ;
            arguments.IsValid = false;
            return;
        }
    }

    arguments.IsValid = true;
    return;
}


function set_dir(v, HiddenId, TextBxId) {

    document.getElementById(HiddenId).value = v;
    if (document.getElementById(HiddenId).value == 0) {
        //alert(TextBxId);
        document.getElementById(TextBxId).style.direction = "ltr";
        document.getElementById(TextBxId).focus();
    }
    else {
        document.getElementById(TextBxId).style.direction = "rtl";
        document.getElementById(TextBxId).focus();
        //alert(TextBxId);
    }

    //alert(document.getElementById(HiddenId).value);

}

